<?php
77777777777777777777777777
include_once('../header.php');	//网站配置
include_once('../check_login.php');	//登录验证
include_once('menu.php');

	$sqla="select * from ".tn('class')." where id = '".$_POST['kefu']."'";
	$kefus = $GLOBALS['db']->get_single($sqla);

	if(empty($kefus)){
		$sqlat="select * from ".tn('class')." where name = '".$_SESSION['mangersession']['manager_name']."'";
		$kefuss = $GLOBALS['db']->get_single($sqlat); 
		$id=$kefuss['id'];
		$num=$kefuss['num'];
		$name=$kefuss['name'];
	}else{
		$id=$kefus['id'];
        $num=$kefus['num'];	
        $name=$kefus['name'];	
	}
	$tpl->assign('name',$name);
	$sql="select * from ".tn('class')." where id = '".$id."'";
	$content = $GLOBALS['db']->get_single($sql);
	$tpl->assign('content',$content);

	$time=date("Ymd",time());
    $where="where 1=1";
    if(!empty($_POST['startdate'])){
      $starttime=str_replace('-','',$_POST['startdate']);	
      $where = $where . " and dates >= '".$starttime."'"; 	
    }
    if(!empty($_POST['enddate'])){
	  $endtime=str_replace('-','',$_POST['enddate']);
	  $endtime=explode(' ',$endtime);  
      $where = $where ." and dates <= '".$endtime."'"; 	
    }
    if(!empty($_POST['kefu'])){
      $where = $where ." and kfnum = '".$num."'"; 	
    }
    if(!empty($_POST['bmobile'])){
      $where = $where ." and bmobile like '%".$_POST['bmobile']."%'"; 	
    } 
    if(empty($_POST['startdate']) && empty($_POST['enddate'])){
      $where = $where ." and dates = '".$time."'"; 		
    }

	$sql_luyin="select * from ".tn('duanxins')." ".$where."";
	$arr = $GLOBALS['db']->get_more($sql_luyin);
	
    //以时间来进行排序
	if(!empty($_POST['group_id']) || $_POST['group_id'] == '0'){
		if($_POST['group_id'] == '0'){
			
		$sort = array(  
			'direction' => 'SORT_ASC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序  
			'field'     => 'filemtime',       //排序字段  
		);  
	
	}else{
		
		$sort = array(  
			'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序  
			'field'     => 'filemtime',       //排序字段  
		);  
	}

	$arrSort = array();  
	foreach($arr AS $uniqid => $row){  
		foreach($row AS $key=>$value){  
			$arrSort[$key][$uniqid] = $value;  
		}  
	}  
	if($sort['direction']){  
		array_multisort($arrSort[$sort['field']], constant($sort['direction']), $arr);  
	}  	
	}

	//以大小来进行排序
	if(!empty($_POST['bigs']) || $_POST['bigs'] == '0' || $_POST['bigs'] == '' && $_POST['goutong'] == '' && $_POST['biaoji'] == '' && $_POST['dianping'] == '' && $_POST['group_id'] == ''){
		if($_POST['bigs'] == '0' || $_POST['bigs'] == '' ){
			
		$sort = array(  
			'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序  
			'field'     => 'sizes',       //排序字段  
		);  
	
	    }else{
		
		$sort = array(  
			'direction' => 'SORT_ASC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序  
			'field'     => 'sizes',       //排序字段  
		);  
	}
	$arrSort = array();  
	foreach($arr AS $uniqid => $row){  
		foreach($row AS $key=>$value){  
			$arrSort[$key][$uniqid] = $value;  
		}  
	}  

	if($sort['direction'] && !empty($arr)){  
		array_multisort($arrSort[$sort['field']], constant($sort['direction']), $arr);  
	}  	
	}
	 foreach($arr as $key=>$vv){
	 
		$sql="select * from ".tn('duanxins')." where filename = '".$vv['filename']."'";
		$content = $GLOBALS['db']->get_single($sql);

		$sqlkf="select * from ".tn('class')." where num = '".$content['kfnum']."'";
		$kfname = $GLOBALS['db']->get_single($sqlkf);

        $arr[$key]['name']=$kfname['name'];
		if(empty($content)){
		 $arr[$key]['zhuangtaima']='4';	
		}else{
		 $arr[$key]['zhuangtaima']=$content['zhuangtai'];	
		}
		$arr[$key]['pjcontent']=$content['pjcontent'];
		$arr[$key]['goutong']=$content['goutong'];
		$arr[$key]['biaoji']=$content['biaoji'];
		$arr[$key]['times']=date("Ymd",$vv['filemtime']);
		$arr[$key]['filemtime']=date("Y-m-d H:i:s",$vv['filemtime']);
		$arr[$key]['key']=$key;	
		
		$filenames=explode('_',$vv['filename']);
        $boda=str_replace('P', '', $filenames[4]);	  //获取拨打电话号码(去除前缀P)
        $boda=str_replace('.wav', '', $boda);	  //获取拨打电话号码(去除后缀.wav)		

		if(empty($boda) || strlen($boda) < 8){
		  $bfsj=$boda;
		  $bodatel= $bfsj;
		  $arr[$key]['bodatel']='暂无';	
		}else{
		  $bfsj=$boda;
		  $bodatel= $bfsj;
		  $arr[$key]['bodatel']=$bodatel;			  
		}		
		
		
        if(!empty($_POST['goutong'])){
		 if($content['goutong'] != $_POST['goutong']){
		 unset($arr[$key]);		 
		 }
		}	
        if(!empty($_POST['biaoji'])){
		 if($content['biaoji'] != $_POST['biaoji']){
		 unset($arr[$key]);		 
		 }
		}
        if(!empty($_POST['dianping'])){
		 if($_POST['dianping'] == '1' && empty($content['pjcontent'])){
		 unset($arr[$key]);		 
		 }
		 if($_POST['dianping'] == '2' && !empty($content['pjcontent'])){
		 unset($arr[$key]);		 
		 }		 
		 }
		 
	 } 	
	array_values($arr);
    //获取个数
	$tpl->assign('nums',count($arr));
	//查询时长	
 	$shichang=0;
	for($i=0;$i<=count($arr);$i++){
	 //$demo= wav_sec('/FileRecord/'.$arr[$i]['dates'].'/'.$num.'/'.$arr[$i]['filename'].'');
	 $demo=$arr[$i]['shichang'];
     $shichang=$shichang + $demo;	  
	} 
	//总时长
	$alltime=Sec2Time($shichang);
	$tpl->assign('alltime',$alltime);

	$tpl->assign('contents',$arr);
	if(empty($_POST['kefu'])){
	    $_POST['kefu']=$id;
	}
	if(empty($_POST['dates'])){
	    $_POST['dates']=date('Y-m-d',time());
	}		
	$tpl->assign('search',$_POST);
	
	$sqlkf="select * from ".tn('class')." where is_show = '1' and kftype='2'";
	$content_kefu = $GLOBALS['db']->get_more($sqlkf);
	$tpl->assign('kefu',$content_kefu);
    $tpl->display('admincp/duanxin/product.html');
	
	
       //获取指定文件（wav）的时长。
	   function wav_sec($file, $base=10) /* {{{ */
	   {
		   $fp = fopen($file, 'r');
		   if (fread($fp,4) != 'RIFF') return 'format not valide';
		  fseek($fp, 20);
		  $rawheader = fread($fp, 16);
		  $header = unpack('vtype/vchannels/Vsamplerate/Vbytespersec/valignment/vbits',$rawheader);
		  $pos = ftell($fp);

		  while (fread($fp, 4) != 'data' && !feof($fp))
		  {
			  $pos++;
			  fseek($fp,$pos);
		  }
		  $rawheader = fread($fp, 4);
		  $data = unpack('Vdatasize',$rawheader);
		  $sec = $data[datasize] / $header[bytespersec];
		  if($base==60)
		  {
			  $seconds = intval($sec % 60);
			  $minutes = intval(($sec - $sec * 60) / 60);
			  return $minutes.':'.$seconds;
		  }
		  else return $sec; 
	  }
     //将秒数转换为时间（年、天、小时、分、秒）
	 function Sec2Time($time){
		if(is_numeric($time)){
		$value = array(
		  "years" => 0, "days" => 0, "hours" => 0,
		  "minutes" => 0, "seconds" => 0,
		);
		if($time >= 31556926){
		  $value["years"] = floor($time/31556926);
		  $time = ($time%31556926);
		}
		if($time >= 86400){
		  $value["days"] = floor($time/86400);
		  $time = ($time%86400);
		}
		if($time >= 3600){
		  $value["hours"] = floor($time/3600);
		  $time = ($time%3600);
		}
		if($time >= 60){
		  $value["minutes"] = floor($time/60);
		  $time = ($time%60);
		}
		$value["seconds"] = floor($time);
		//return (array) $value;
		//$t=$value["years"] ."年". $value["days"] ."天"." ". $value["hours"] ."小时". $value["minutes"] ."分".$value["seconds"]."秒";
		$t=" ". $value["hours"] ."小时". $value["minutes"] ."分".$value["seconds"]."秒";
		Return $t;
		
		 }else{
		return (bool) FALSE;
		}
	 }  
  
?>